package cn.enjoy.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author K
 * @date 2022-07-20 22:17
 */
@Configuration
public class MqQueueConfig {


    public static final String QUEUE_ERROR = "error.queue";
    public static final String QUEUE_ONE = "direct.queue.1";
    public static final String QUEUE_TWO = "direct.queue.2";
    public static final String QUEUE_WORK = "work.queue";


    public static final String EXCHANGE_ERROR = "error.exchange";
    public static final String EXCHANGE_DIRECT = "direct.mode";

    @Bean
    public Queue errorQueue() {
        return new Queue(QUEUE_ERROR);
    }

    @Bean
    public Queue workQueue() {
        return new Queue(QUEUE_WORK);
    }

    @Bean
    Binding errorBinding(DirectExchange errorExchange, Queue errorQueue) {
        return BindingBuilder
                .bind(errorQueue)
                .to(errorExchange)
                .with(QUEUE_ERROR);
    }

    @Bean
    DirectExchange errorExchange() {
        return ExchangeBuilder
                .directExchange(EXCHANGE_ERROR)
                .durable(true)
                .build();
    }


    /**
     * fanout广播模式 队列1
     */
    @Bean
    public Queue fanoutQueueOne() {
        return new Queue("fanout.queue.1");
    }

    /**
     * fanout广播模式 队列2
     */
    @Bean
    public Queue fanoutQueueTwo() {
        return new Queue("fanout.queue.2");
    }

    /**
     * fanout广播模式 交换机
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout.mode");
    }

    /**
     * fanout 交换机绑定队列
     *
     * @param fanoutQueueOne 广播模式 队列1
     * @param fanoutExchange 广播模式 交换机
     * @return Binding
     */
    @Bean
    public Binding fanoutBinding1(Queue fanoutQueueOne, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueueOne).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutBinding2(Queue fanoutQueueTwo, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueueTwo).to(fanoutExchange);
    }


    /**
     * direct 路由模式
     */
    @Bean
    public Queue directQueueOne() {
        return new Queue("direct.queue.1");
    }

    @Bean
    public Queue directQueueTwo() {
        return new Queue("direct.queue.2");
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_DIRECT);
    }

    @Bean
    public Binding directBinding1(Queue directQueueOne, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueOne).to(directExchange).with(QUEUE_ONE);
    }

    @Bean
    public Binding directBinding2(Queue directQueueTwo, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueueTwo).to(directExchange).with(QUEUE_TWO);
    }


    /**
     * topic动态路由模式 队列1
     */
    @Bean
    public Queue topicQueueOne() {
        return new Queue("topic.queue.1");
    }

    @Bean
    public Queue topicQueueTwo() {
        return new Queue("topic.queue.2");
    }

    /**
     * topic动态路由模式 交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topic.mode");
    }

    /**
     * topic动态路由模式 队列绑定交换机
     *
     * @param topicQueueOne topic动态路由模式 队列2
     * @param topicExchange topic动态路由模式 交换机
     * @return Binding
     */
    @Bean
    public Binding topicBinding1(Queue topicQueueOne, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueueOne).to(topicExchange).with("log.info");
    }

    @Bean
    public Binding topicBinding2(Queue topicQueueTwo, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueueTwo).to(topicExchange).with("log.#");
    }

    /**
     * TTL过期队列
     */
    @Bean
    public Queue ttlQueue() {
        HashMap<String, Object> args = new HashMap<>();
        // 10秒过期
        args.put("x-message-ttl", 10000);
        return new Queue("ttl.queue", true, false, false, args);
    }

}
